<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex' // 假设使用 Vuex

// 假设这是你的请求函数
const request = {
  get(url) {
    return new Promise((resolve, reject) => {
      // 模拟请求返回数据
      resolve({ code: '0', data: { id: 1, name: 'test' } })
    }).catch((error) => {
      console.error('请求失败:', error)
    })
  }
}

const user = ref({})
const router = useRouter()
const store = useStore()

onMounted(() => {
  const userStr = sessionStorage.getItem('user') || '{}'
  user.value = JSON.parse(userStr)

  if (user.value.id) {
    request.get('/user/' + user.value.id).then(res => {
      if (res.code === '0') {
        user.value = res.data
        store.commit('setUser', res.data) // 假设使用 Vuex 存储用户数据
      }
    }).catch((error) => {
      console.error('获取用户信息失败:', error)
    })
  }
})
</script>

<template>
  <div>
    <el-menu style="width: 200px; min-height: calc(100vh - 50px)" :default-active="$route.path" router>
      <div v-for="m in user.permissions" :key="m.id">
        <el-menu-item :index="m.path" v-if="m.name!== 'Person' && m.name!== 'Password'">
          <i :class="m.icon"></i> {{ m.comment }}
        </el-menu-item>
      </div>
    </el-menu>
  </div>
</template>


<style scoped></style>